<?php

namespace App\Http\Controllers\Main;

use App\Casecate;
use App\Cases;
use App\CrmCase;
use App\CrmCasePicture;
use App\CrmIndustry;
use App\CrmIndustrys;
use App\CrmProduct;
use App\CrmProductCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class CaseController extends Controller
{
//    public function index()
//    {
//        $case = CrmCase::where('status', 1)->orderBy('rank', 'desc')->orderBy('update_time', 'desc')->paginate(20);
//        $color = 4;
//        return view('main/case',compact('color','case'));
//    }
/********************************************/
    public function index()
    {
        #获取15条重要案例
        $c = CrmCase::where('status',1)->orderBy('rank','DESC')->orderBy('create_time','DESC')->take(15)->get();
        #接收参数
        $take = 30;
        $type = Input::get('type');
        $service = Input::get('service');
        $industry = Input::get('industry');
        $keyword = Input::get('keyword');

        $obj = new CrmCase();
        $id = array();
        $three = array();
        if(!empty($type)){
            #查服务
            $data = CrmProduct::where('category',$type)->get();
            foreach ($data as $k=>$d){
                $three[$k]['name'] = $d->name;
                $three[$k]['id'] = $d->id;
                $id[] = $d->id;
            }
            $obj = $obj->whereIn('product_id',$id);
//            if(!empty($service)){
//                $obj = $obj->whereIn('product_id',$id);
//            }

        }
        if(!empty($service)){
            $obj = $obj->where('product_id',$service);
        }
        if(!empty($industry)){
            $obj = $obj->where('industry',$industry);
        }
        if(!empty($keyword)){
            $obj = $obj->where(function($query)use($keyword){
                $query->where('name','like',"%$keyword%")
                ->orWhere('brief','like',"%$keyword%");
            });
        }
        $case = $obj->where('status',1)->orderBy('rank','DESC')->orderBy('create_time','DESC')->paginate($take);
        $count = $obj->where('status',1)->count();
        #获取案例图片
        $picture = array();
        foreach ($case as $k=>$v){
            $picture[$k] = CrmCasePicture::where('link_id',$v->id)
                ->orderBy('rank','desc')
                ->orderBy('create_time','desc')
                ->get();
        }
        #获取三级分类
        $category = $this->getCategory();
        #获取行业
        $industrys = CrmIndustry::all();
        $color = 4;
        Input::get('page')?$page=Input::get('page'):$page=1;
        #获取品牌平面设计
        $default = CrmProduct::where('category',12)->get();
        return view('main/case',
            compact(
                'case',
                'color',
                'picture',
                'category',
                'industrys',
                'page',
                'count',
                'take',
                'c',
                'three',
                'default',
                'type',
                'service',
                'industry',
                'keyword'
            ));

    }

    public function getCategory()
    {
        $product_category = CrmProductCategory::select('id', 'name')->where('parent_id', '0')->where('status', '1')->get()->toArray();
        for($i = 0; $i < sizeof($product_category); $i++) {
            $id = $product_category[$i]['id'];
            $product_category[$i]['son'] = CrmProductCategory::select('id', 'name')->where('parent_id', $id)->where('status', '1')->get()->toArray();
            for($j = 0; $j < sizeof($product_category[$i]['son']); $j++) {
                $id = $product_category[$i]['son'][$j]['id'];
                $product_category[$i]['son'][$j]['products'] = CrmProduct::select('id', 'name')->where('category', $id)->where('status', '1')->get()->toArray();
            }
        }
        return $product_category;
    }

    public function detail($id)
    {
        #获取信息
        $detail = CrmCase::where('id',$id)->first();
        $detail->edit = str_replace('<img src="/','<img src="http://manage.vipyipoint.com/',$detail->edit);
        $detail->edit = str_replace('<img src="http://www.vipyidian.com/','<img src="http://www.vipyipoint.com/',$detail->edit);
        $detail->edit = str_replace('<img src="http://vipyidian.com/','<img src="http://www.vipyipoint.com/',$detail->edit);
        #获取5条相关案例
        $s = CrmCase::where('product_id',$detail->product_id)
            ->where('id','!=',$id)
            ->orderBy('rank','DESC')
            ->orderBy('create_time','DESC')
            ->take(5)
            ->get();

        #获取左栏数据
        $productCate = $this->productCate();
        $color = 4;
        return view('main/case/detail',compact('detail','color','s','productCate'));
    }

}
